package org.thingsboard.server.dao.jpa.imp;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.repository.CrudRepository;
import org.thingsboard.server.dao.jpa.TDevice;
import org.thingsboard.server.dao.jpa.dto.TDeviceDto;
import org.thingsboard.server.dao.util.SqlDao;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import java.util.List;

@SqlDao
public interface TDeviceRepository extends CrudRepository<TDevice, String>,JpaSpecificationExecutor<TDevice> {
    List<TDevice> findTDevicesByGatewayCode(String gatewayCode);

    TDevice findByDeviceCode(String deviceCode);

    @Query("SELECT new org.thingsboard.server.dao.jpa.dto.TDeviceDto(a.deviceId,a.deviceCode,a.deviceName,a.deviceCode,a.modelCode,a.companyCode,a.gatewayCode,a.deviceStatus,a.deviceArg,c.areaName) FROM TDevice a LEFT JOIN TAreaDevice b ON a.deviceId=b.deviceId LEFT JOIN TArea c ON b.areaId=c.areaId  WHERE  c.areaId IN :areas")
    Page<TDeviceDto> findTDEvicePage(@Param("areas")List<String> areas, Pageable pageable);

    @Query("SELECT new org.thingsboard.server.dao.jpa.dto.TDeviceDto(a.deviceId,a.deviceCode,a.deviceName,a.deviceCode,a.modelCode,a.companyCode,a.gatewayCode,a.deviceStatus,a.deviceArg,c.areaName) FROM TDevice a LEFT JOIN TAreaDevice b ON a.deviceId=b.deviceId LEFT JOIN TArea c ON b.areaId=c.areaId")
    Page<TDeviceDto> findTDEvicePage( Pageable pageable);

}
